package com.xinjiang.electricpower.service.impl;

import com.ruoyi.common.core.utils.StringUtils;
import com.xinjiang.electricpower.domain.DTO.WorkOrderIndexDto;
import com.xinjiang.electricpower.domain.vo.*;
import com.xinjiang.electricpower.mapper.WorkOrderIndexMapper;
import com.xinjiang.electricpower.service.IUserAuthService;
import com.xinjiang.electricpower.service.IWorkOrderIndexService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author zhanghui
 * @date 2023/2/11 15:33
 * @Description
 */
@Service
@Slf4j
public class WorkOrderIndexServiceImpl implements IWorkOrderIndexService {

    @Autowired
    private WorkOrderIndexMapper workOrderIndexMapper;

    @Autowired
    private IUserAuthService iUserAuthService;

    @Override
    public List<TodoOrderInfoVo> getTodoOrderInfo(WorkOrderIndexDto workOrderIndexDto) {
        //权限参数
        setAuthParam(workOrderIndexDto);
        List<TodoOrderInfoVo> newList = new ArrayList<>();
        List<TodoOrderInfoVo> list = workOrderIndexMapper.getTodoOrderInfo(workOrderIndexDto);
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map<String, TodoOrderInfoVo> collect = list.stream().collect(Collectors.toMap(TodoOrderInfoVo::getOrderNo, Function.identity(), (c1, c2) -> {
            try {
                return sdf.parse(c1.getBeginDate()).getTime() > sdf.parse(c2.getBeginDate()).getTime() ? c1 : c2;
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }));
        newList = new ArrayList<>(collect.values());
        return newList;
    }

    @Override
    public Map<String, Object> getAlertWorkOrderNum(WorkOrderIndexDto workOrderIndexDto) {
        Map<String, Object> resultMap = new HashMap<>();
        //权限参数
        setAuthParam(workOrderIndexDto);
        List<YjOrderCountVo> alertWorkOrderNum = workOrderIndexMapper.getAlertWorkOrderNum(workOrderIndexDto);
        //预警工单数据统计
        AlertWorkOrderNumVo alertWorkOrderNumVo = new AlertWorkOrderNumVo();
        Integer total = 0;
        for (YjOrderCountVo yjOrderCountVo : alertWorkOrderNum) {
            //紧急级别 由高到低
            if ("5".equals(yjOrderCountVo.getYjType())){
                alertWorkOrderNumVo.setDangerNum(yjOrderCountVo.getCount());
            }
            if ("4".equals(yjOrderCountVo.getYjType())){
                alertWorkOrderNumVo.setWarningNum(yjOrderCountVo.getCount());
            }
            if ("2".equals(yjOrderCountVo.getYjType())){
                alertWorkOrderNumVo.setCommonNum(yjOrderCountVo.getCount());
            }
            if ("1".equals(yjOrderCountVo.getYjType())){
                alertWorkOrderNumVo.setNotUrgentNum(yjOrderCountVo.getCount());
            }
            total = total + yjOrderCountVo.getCount();
        }
        alertWorkOrderNumVo.setSumNum(total);

        List<AlertWorkOrderPerNumVo> alertWorkOrderPerNumVos = new ArrayList<>();
        List<YjOrderCountVo> alertWorkOrderPerNum = workOrderIndexMapper.getAlertWorkOrderPerNum(workOrderIndexDto);
        List<YjOrderCountVo> filterList = alertWorkOrderPerNum.stream().filter(n -> StringUtils.isNotEmpty(n.getAssigner())).collect(Collectors.toList());
        Map<String, List<YjOrderCountVo>> collect = filterList.stream().collect(Collectors.groupingBy(YjOrderCountVo::getAssigner));
        for(String key : collect.keySet()){
            Integer sumCount = 0;
            for (YjOrderCountVo yjOrderCountVo : collect.get(key)) {
                AlertWorkOrderPerNumVo perNumVo = new AlertWorkOrderPerNumVo();
                //紧急级别 由高到低
                if ("5".equals(yjOrderCountVo.getYjType())){
                    perNumVo.setDangerNum(yjOrderCountVo.getCount());
                }
                if ("4".equals(yjOrderCountVo.getYjType())){
                    perNumVo.setWarningNum(yjOrderCountVo.getCount());
                }
                if ("2".equals(yjOrderCountVo.getYjType())){
                    perNumVo.setCommonNum(yjOrderCountVo.getCount());
                }
                if ("1".equals(yjOrderCountVo.getYjType())){
                    perNumVo.setNotUrgentNum(yjOrderCountVo.getCount());
                }
                sumCount = sumCount + yjOrderCountVo.getCount();
                perNumVo.setSumNum(sumCount);
                perNumVo.setAssignerName(yjOrderCountVo.getAssignerName());
                perNumVo.setAssigner(yjOrderCountVo.getAssigner());
                alertWorkOrderPerNumVos.add(perNumVo);
            }
        }
        resultMap.put("yjUpper", alertWorkOrderNumVo);
        resultMap.put("yjDown", alertWorkOrderPerNumVos);
        return resultMap;
    }

    @Override
    public List<AlertWorkOrderPerNumVo> getAlertWorkOrderPerNum(String consNo) {
        return null;
    }


    /**
     * 设置权限参数
     * @param workOrderIndexDto
     */
    private void setAuthParam(WorkOrderIndexDto workOrderIndexDto){
        AuthInfoVo authStr = iUserAuthService.getAuthStr();
        workOrderIndexDto.setOrgNo(authStr.getOrgNo());
        workOrderIndexDto.setConsNo(authStr.getConsNo());
    }
}
